#include "tab1-lua.h"
#include "ImGuiFileDialog.h"
#include "imgui.h"
#include "mgr.h"

using namespace std;

extern ImVec2 btn_size;
extern ImVec4 success_color;
extern ImVec4 info_color;
#define FILE_ROOT_PATH "C:\\wow\\classic_wlk\\script"

static ScriptState current_state = IDLE;
// 全局变量存储计时器状态
static bool is_timing = false;
static std::chrono::steady_clock::time_point start_time;
static uint32 elapsed_seconds{0};

void show_timer() {
  if (is_timing) {
    auto now = chrono::steady_clock::now();
    elapsed_seconds =
        chrono::duration_cast<chrono::seconds>(now - start_time).count();
    ImGui::TextColored(success_color, "运行时间: %d 秒", elapsed_seconds);
    ImGui::SameLine();
    ImGui::TextColored(success_color, "剩余时间: %d 秒",
                       api()->get_time() - elapsed_seconds);
  } else {
    ImGui::TextColored(info_color, "未运行");
  }
}

void script_execute() {
  // 时间设置部分
  static int hour = 7;
  static int minute = 30;
  ImGui::SliderInt("小时", &hour, 0, 23);
  ImGui::SliderInt("分钟", &minute, 0, 59);
  // FIXME: 要销毁线程池, 所以每次开始后都必须点结束脚本
  // reset_script_state();
  // ImGui::SameLine();
  show_timer();
  // 脚本执行部分
  ImGui::SetNextWindowSize(ImVec2{800, 600}, ImGuiCond_FirstUseEver);
  if (current_state == IDLE || current_state == STOPPED) {
    if (ImGui::Button("执行脚本", btn_size)) {
      IGFD::FileDialogConfig config;
      config.path = FILE_ROOT_PATH;
      ImGuiFileDialog::Instance()->OpenDialog("ChooseFileDlgKey", "选择文件",
                                              ".lua,.*", config);
    }
  } else {
    // 不可用状态显示灰色按钮
    ImGui::BeginDisabled();
    ImGui::Button("执行脚本", btn_size);
    ImGui::EndDisabled();
  }
  if (ImGuiFileDialog::Instance()->Display("ChooseFileDlgKey")) {
    is_timing = false;
    elapsed_seconds = 0;
    if (ImGuiFileDialog::Instance()->IsOk()) {
      std::string filePathName = ImGuiFileDialog::Instance()->GetFilePathName();
      std::string filePath = ImGuiFileDialog::Instance()->GetCurrentPath();
      ImGui::Text("选择的文件:%s", filePath.c_str());
      api()->set_time(hour * 60 * 60 + minute * 60);
      Mgr::get()->run_worker(filePathName, FILE_ROOT_PATH);
      is_timing = true;
      start_time = chrono::steady_clock::now();
      current_state = RUNNING;
    }
    ImGuiFileDialog::Instance()->Close();
  }
  ImGui::SameLine();
}

// 只有在RUNNING状态时按钮可用
void script_suspend() {
  if (current_state == RUNNING) {
    if (ImGui::Button("暂停脚本", btn_size)) {
      Mgr::get()->suspend_pool();
      current_state = PAUSED; // 切换到暂停状态
    }
  } else {
    ImGui::BeginDisabled();
    ImGui::Button("暂停脚本", btn_size);
    ImGui::EndDisabled();
  }
  ImGui::SameLine();
}

// 只有在PAUSED状态时按钮可用
void script_resume() {
  if (current_state == PAUSED) {
    if (ImGui::Button("恢复脚本", btn_size)) {
      Mgr::get()->resume_pool();
      current_state = RUNNING; // 切换到运行状态
    }
  } else {
    ImGui::BeginDisabled();
    ImGui::Button("恢复脚本", btn_size);
    ImGui::EndDisabled();
  }
  ImGui::SameLine();
}

void script_stop() {
  // 只有在RUNNING或PAUSED状态时按钮可用
  if (current_state == RUNNING || current_state == PAUSED) {
    if (ImGui::Button("结束脚本", btn_size)) {
      Mgr::get()->stop_all_scripts();
      is_timing = false;
      elapsed_seconds = 0;
      current_state = STOPPED; // 切换到结束状态
    }
  } else {
    ImGui::BeginDisabled();
    ImGui::Button("结束脚本", btn_size);
    ImGui::EndDisabled();
  }
}

void reset_script_state() {
  if (ImGui::Button("重置状态")) {
    current_state = IDLE;
    is_timing = false;
    elapsed_seconds = 0;
  }
}
